package com.example.factory.api.facade.impl;

import com.example.factory.api.dto.DtoReportGarding;
import com.example.factory.api.dto.Response;
import com.example.factory.api.dto.assembler.ReportGardingAssmbler;
import com.example.factory.api.dto.form.DtoReportGardingForm;
import com.example.factory.api.facade.ReporterGradingFacade;
import com.example.factory.core.domain.*;
import com.example.factory.core.repository.ReportEquipmentRepository;
import com.example.factory.core.repository.ReportLevel1DimensionRepository;
import com.example.factory.core.repository.ReportLevel2DimensionRepository;
import com.example.factory.core.repository.ReportPlanRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lei Tao
 */
@Service
public class ReporterGradingFacadeImpl implements ReporterGradingFacade {
    private final ReportGardingAssmbler reportGardingAssmbler;
    private final ReportLevel1DimensionRepository reportLevel1DimensionRepository;
    private final ReportLevel2DimensionRepository reportLevel2DimensionRepository;
    private final ReportPlanRepository reportPlanRepository;
    private final ReportEquipmentRepository reportEquipmentRepository;


    public ReporterGradingFacadeImpl(ReportGardingAssmbler reportGardingAssmbler, ReportLevel1DimensionRepository reportLevel1DimensionRepository, ReportLevel2DimensionRepository reportLevel2DimensionRepository, ReportPlanRepository reportPlanRepository, ReportEquipmentRepository reportEquipmentRepository) {
        this.reportGardingAssmbler = reportGardingAssmbler;
        this.reportLevel1DimensionRepository = reportLevel1DimensionRepository;
        this.reportLevel2DimensionRepository = reportLevel2DimensionRepository;
        this.reportPlanRepository = reportPlanRepository;
        this.reportEquipmentRepository = reportEquipmentRepository;
    }

    @Override
    @Transactional
    public Response<DtoReportGarding> save(DtoReportGardingForm dtoReportGardingForm) {
        ReportLevel1Dimension reportLevel1Dimension = reportGardingAssmbler.toReportLevel1Dimension(dtoReportGardingForm);
        ReportPlan reportPlan = reportLevel1Dimension.getReportPlan();
        ReportEquipment reportEquipment = reportEquipmentRepository.findByGuidAndArchivedFalse(dtoReportGardingForm.getReportEquipmentGuid());
        List<ReportEquipment> reportEquipments = reportPlan.getReportEquipments();
        if (reportEquipments == null) {
            reportEquipments = new ArrayList<>();
        }
        reportEquipment.setReportEquipmentStatus(ReportEquipmentStatus.OVER);
        reportLevel1DimensionRepository.save(reportLevel1Dimension);
        reportEquipments.add(reportEquipment);
        reportPlan.setReportLevel1Dimension(reportLevel1Dimension);
        reportEquipment.setReportLevel1Dimension(reportLevel1Dimension);
        reportLevel1Dimension.setReportPlan(reportPlan);
        boolean flag = true;
        for (ReportEquipment reportEquipment1 : reportEquipments) {
            if (ReportEquipmentStatus.CREATE.equals(reportEquipment1.getReportEquipmentStatus())) {
                flag = false;
                break;
            }
        }
        if (flag) {
            reportPlan.setReportStatus(ReportStatus.AUDIT);
        }
        reportPlanRepository.save(reportPlan);
        reportEquipmentRepository.saveAll(reportEquipments);

        List<ReportLevel2Dimension> reportLevel2Dimensions = reportLevel1Dimension.getReportLevel2Dimensions();
        for (ReportLevel2Dimension reportLevel2Dimension : reportLevel2Dimensions) {
            reportLevel2Dimension.setReportLevel1Dimension(reportLevel1Dimension);
            reportLevel2DimensionRepository.save(reportLevel2Dimension);
        }
        DtoReportGarding dtoReportGarding = reportGardingAssmbler.toDtoReportGarding(reportLevel1Dimension);
        return Response.success(dtoReportGarding);

    }
}
